<?php

namespace app\home\controller;

use app\common\controller\Home;

class Comment extends Home
{
	// 
	public function __construct(){
		parent::__construct();
		
		$this->UserModel = model('User.User');
		
		$this->RecordModel = model('User.Record');
		
		$this->CommentModel = model('Post.Comment');
		
		$this->PostModel = model('Post.Post');
	}
	
	// 添加评论
	public function add(){
		if($this->request->isPost())
		{
			// 接收评论内容
			$content = $this->request->param('content');

			// 用户id
			$userid = isset($this->LoginUser['id']) ? $this->LoginUser['id'] : 0;

			// 评论上一级id
			$pid = $this->request->param('pid','0');

			// 帖子id
			$postid = $this->request->param('postid');

			$post = model('Post.Post')->find($postid);

			if(!$post)
			{
				$this->error('当前评论帖子不存在');
			}

			$data = [
				'content' => $content,
				'userid' => $userid,
				'postid' => $postid,
				'pid' => $pid,
				'accept' => 0
			];

			$comment = $this->CommentModel->find($pid);

			if($comment)
			{
				$data['level'] = $comment['level'] + 1;
			}else{
				$data['level'] = 0;
			}

			$result = $this->CommentModel->validate('common/Post/Comment')->save($data);


			if($result === FALSE)
			{
				$this->error('评论失败');
			}else{
				$this->success('评论成功',url('home/index/info',['postid'=>$postid]),null,0);
			}
		}
	}
	
	// 采纳
	public function accept()
	{
		if($this->request->isAjax())
		{
			$comid = $this->request->param('comid');

			$comment = $this->CommentModel->with('post')->find($comid);

			if(!$comment)
			{
				$this->error('该评论不存在，无法采纳');
			}

			/* 
				评论表 => accept = 1
				帖子表 => accept = userid
				用户表 => accept => userid = 加积分
				消费记录表 => accept => userid = 新增记录
			*/

			// 开启事务 => 操作多个表 => 更新或者新增 => 当某个表新增或者更新失败，它可以让操作的表撤回新增或者更新的数据
			$this->CommentModel->startTrans();
			$this->UserModel->startTrans();
			$this->PostModel->startTrans();
			$this->RecordModel->startTrans();

			// 更新评论表数据
			$CommentData = [
				'id' => $comid,
				'accept' => 1
			];

			$CommentStatus = $this->CommentModel->isUpdate(true)->save($CommentData);

			if($CommentStatus === FALSE)
			{
				$this->error($this->CommentModel->getError());
			}

			// 获取悬赏积分 => 获取用户的积分
			$User = $this->UserModel->find($comment['userid']);

			if(!$User)
			{
				$this->error('该用户不存在');
			}

			$UpdatePonit = bcadd($User['point'],$comment['post']['point']);

			$UserData = [
				'id' => $comment['userid'],
				'point' => $UpdatePonit
			];

			$UserStatus = $this->UserModel->isUpdate(true)->save($UserData);

			if($UserStatus === FALSE)
			{
				$this->CommentModel->rollback();
				$this->error($this->UserModel->getError);
			}

			// 帖子
			$PostData = [
				'id' => $comment['postid'],
				'accept' => $comment['userid']
			];

			$PostStatus = $this->PostModel->isUpdate(true)->save($PostData);
			
			if($PostStatus === FALSE)
			{
				$this->CommentModel->rollback();
				$this->UserModel->rollback();
				$this->error($this->PostModel->getError);
			}

			// 消费记录
			$RecordData = [
				'point' => $comment['post']['point'],
				'content' => "您在【{$comment['post']['title']}】里评论内容被采纳了，获得了{$comment['post']['point']}积分",
				'state' => 2,
				'userid' => $comment['userid']
			];

			$RecordStatus = $this->RecordModel->validate('common/User/Record')->save($RecordData);

			if($RecordStatus === FALSE)
			{
				$this->CommentModel->rollback();
				$this->UserModel->rollback();
				$this->PostModel->rollback();
				$this->error($this->RecordModel->getError);
			}

			if($RecordStatus === FALSE || $CommentStatus === FALSE || $UserStatus === FALSE || $PostStatus === FALSE)
			{
				$this->CommentModel->rollback();
				$this->UserModel->rollback();
				$this->PostModel->rollback();
				$this->RecordModel->rollback();
				$this->error('采纳失败');
			}else{
				$this->CommentModel->commit();
				$this->UserModel->commit();
				$this->PostModel->commit();
				$this->RecordModel->commit();
				$this->success('采纳成功');
			}

		}
	}
	
	
	// 点赞
	public function like(){
		
		if($this->request->isAjax()){
			
			// 当前用户id
			// 浏览量按id进行更新
			$userid = !empty($this->LoginUser['id']) ? $this->LoginUser['id'] : 0;
			
			// 查询数据库是否有该用户id
			$User = $this->UserModel->find($userid);
			
			// 评论id
			$comid = $this->request->param('comid');
			
			// 获取评论
			$comment = $this->CommentModel->where('id',$comid)->find();
			
			// 判断当前点赞用户是否已经点赞
			// 字符串转数组
			$LikeStr = explode(',',$comment['like']);
			
			if(!$comment)
			{
				$this->error('该评论不存在，无法点赞');
			
			}
			// 点赞提示
			$result = null;
			// 取消提示
			// $results = null;
			if($User){
				// 判断在点赞中是否存在该用户id
				if(!in_array($User['id'],$LikeStr)){
					// 加进数组
					$LikeStr[] = $User['id'];
					
					// 再进行字符串转换
					$LikeStr = implode(',',$LikeStr);
					
					// 把字符串的左边逗号去掉
					$LikeStr = ltrim($LikeStr,',');
					
					// 根据评论id存储进去
					$result = $this->CommentModel->where('id',$comid)->update(['like' => $LikeStr]);
					// 点赞次数
					// $LikeStr = !empty($LikeStr) ? count($LikeStr) : 1;
					if($result === FALSE){
						$this->error('点赞失败');
					}else{
						$this->success('点赞成功');
						
					}
					
				}else{
					// halt('取消点赞');
					// 字符串转数组  所有点赞id
					$LikeStr = explode(',',$comment['like']);
					// 取消点赞
					foreach($LikeStr as $k=>$v){
					
						if($v == $User['id']){
						
							unset($LikeStr[$k]);
						
						}
					
					}
					// 将数组重新转换成字符串
					// 再进行字符串转换
					$LikeStr = implode(',',$LikeStr);
					// 重新更新点赞
					$result = $this->CommentModel->where('id',$comid)->update(['like' => $LikeStr]);
					
					if($result === FALSE){
						$this->error('取消失败');
					}else{
						$this->success('取消成功');
						
					}
					
				}
				
			}
			
		}
	}
	
	// 删除评论功能
	public function delcom(){
		
		if($this->request->isAjax()){
			
			// 评论id
			$comid = $this->request->param('comid');
			
			// 查询
			$commentitem = $this->CommentModel->where('id',$comid)->find();
			
			if(!$commentitem){
				$this->error('该评论不存在');
			}
			// 删除自己
			$result = model('Post.Comment')->destroy(['id' => $comid]);
			// 删除子集
			comment_list($comid);
			
			if($result === FALSE)
			{
				$this->error('删除失败');
			}
			
			$this->success('删除成功',url('home/index/info'));
			
		}
		
	}
}